﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;
using System.Text;
using System.Text.RegularExpressions;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class JingjiaController : BaseController
    {
        [Description("竞价配置列表")]
        [ViewPage]
        public ActionResult GetConfigList(JingjiaConfigQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_JingjiaConfig.Where(t => t.is_del == 0);
            if (queryModel.jingjia_config_satus != -10)
            {
                queryable = queryable.Where(t => t.jingjia_config_satus == queryModel.jingjia_config_satus);
            }

            if (queryModel.jingjia_config_id > 0)
            {
                queryable = queryable.Where(t => t.jingjia_config_id == queryModel.jingjia_config_id);
            }


            if (!string.IsNullOrWhiteSpace(queryModel.jingjia_config_name))
                queryable = queryable.Where(t => (t.jingjia_config_name.Contains(queryModel.jingjia_config_name)));

            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.begin_time >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.begin_time < end_time);
            }

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.jingjia_config_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_JingjiaConfig> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_JingjiaConfig>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }


        [Description("编辑竞价配置")]
        [ViewPage]
        public ActionResult AddConfig(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_JingjiaConfig model = new ADZ_JingjiaConfig();
            if (id > 0)
            {
                model = dbcontext.ADZ_JingjiaConfig.FirstOrDefault(t => t.jingjia_config_id == id && t.is_del == 0);
            }
            return View(model);
        }

        [Description("编辑竞价配置")]
        [HttpPost]
        public ActionResult AddConfig(ADZ_JingjiaConfig model)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            //参数验证
            if (string.IsNullOrWhiteSpace(model.jingjia_config_name))
            {
                resMsg.Message = "竞价名称不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (model.baozheng_price <= 0)
            {
                resMsg.Message = "保证金填写错误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (model.begin_time == DateTime.MinValue)
            {
                resMsg.Message = "竞价开始时间错误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (model.end_time == DateTime.MinValue)
            {
                resMsg.Message = "竞价结束时间错误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (string.IsNullOrWhiteSpace(model.remark))
                model.remark = "";


            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (model.jingjia_config_id > 0)
            {
                var old_model = dbcontext.ADZ_JingjiaConfig.FirstOrDefault(t => t.jingjia_config_id == model.jingjia_config_id);
                if (old_model != null)
                {
                    List<string> explist = new List<string>();
                    explist.Add("jingjia_config_id");
                    explist.Add("create_time");
                    explist.Add("baoming_counts");
                    explist.Add("is_show");
                    Utils.Mapper(old_model, model, explist);
                }

                dbcontext.SaveChanges();

            }
            else
            {
                model.create_time = DateTime.Now;
                model.is_del = 0;
                dbcontext.ADZ_JingjiaConfig.Add(model);
            }

            dbcontext.SaveChanges();

            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="jingjia_config_id"></param>
        /// <param name="up_down">=1 发布  =0 撤销</param>
        /// <returns></returns>
        [Description("竞价发布到APP端")]
        [HttpPost]
        public ActionResult PublishConfig(long jingjia_config_id, int up_down)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_JingjiaConfig model = dbcontext.ADZ_JingjiaConfig.FirstOrDefault(t => t.jingjia_config_id == jingjia_config_id);
            if (model != null)
            {
                //上架
                if (up_down == 1)
                {
                    var list = dbcontext.ADZ_JingjiaConfig.Where(t => t.is_show == 1).ToList();
                    if (list.Any())
                    {
                        list.ForEach(t =>
                        {
                            t.is_show = 0;
                        });
                    }
                    model.is_show = 1;
                    ReleaseBiz.Instance.SetNewReleaseNo(ReleaseListType.Jingjia);
                }
                else
                {
                    model.is_show = 0;
                }
                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("编辑配置竞价包")]
        [ViewPage]
        public ActionResult editconfigpackage(JingjiaQueryModel queryModel)
        {
            queryModel.Trim();
            queryModel.pageSize = 1000;
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_JingjiaMaster.Where(t => t.is_del == 0);
            if (queryModel.jingjia_id > 0)
            {
                queryable = queryable.Where(t => t.jingjia_id == queryModel.jingjia_id);
            }
            if (queryModel.jingjia_satus != -10)
            {
                queryable = queryable.Where(t => t.jingjia_satus == queryModel.jingjia_satus);
            }

            queryable = queryable.Where(t => t.jingjia_config_id == 0);

            ////成交未付款
            if (queryModel.is_notpay == 1)
            {
                var tt = dbcontext.ADZ_JingjiaSubmit.Where(t => t.is_hit == 1 && t.status != 10).ToList();
                var jingjia_id_list = new List<long>();
                tt.ForEach(t =>
                {
                    jingjia_id_list.Add(t.jingjia_id);
                });
                queryable = queryable.Where(t => t.jingjia_satus == 2 && t.chujia_counts > 0).Where(t => jingjia_id_list.Contains(t.jingjia_id));
            }

            if (!string.IsNullOrWhiteSpace(queryModel.jingjia_name))
                queryable = queryable.Where(t => (t.jingjia_name.Contains(queryModel.jingjia_name)));
            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.create_time >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.create_time < end_time);
            }

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.jingjia_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_JingjiaMaster> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_JingjiaMaster>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }


        [Description("编辑配置竞价包")]
        [HttpPost]
        public ActionResult doeditjingjiapackage(int inout, long jingjia_config_id, long jingjia_id)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (inout == 0 || jingjia_config_id <= 0 || jingjia_config_id <= 0)
            {
                resMsg.Message = "参数有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (inout == 1)//移除
            {
                var jingjiagoods = dbcontext.ADZ_JingjiaMaster.Where(t => t.jingjia_config_id == jingjia_config_id && t.jingjia_id == jingjia_id).ToList();
                if (jingjiagoods.Any())
                {
                    jingjiagoods.ForEach(t => t.jingjia_config_id = 0);
                }
                dbcontext.SaveChanges();
            }
            else if (inout == 2)//加入
            {
                var jingjiagoods = dbcontext.ADZ_JingjiaMaster.Where(t => t.jingjia_id == jingjia_id && t.jingjia_config_id == 0).ToList();
                if (jingjiagoods.Any())
                {
                    jingjiagoods.ForEach(t => t.jingjia_config_id = jingjia_config_id);
                }
                dbcontext.SaveChanges();
            }
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        [Description("批量编辑配置竞价包")]
        [HttpPost]
        public ActionResult doeditjingjiapackagemore(int inout, string jingjia_ids, long jingjia_config_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (inout == 0 || string.IsNullOrWhiteSpace(jingjia_ids) || jingjia_config_id <= 0)
            {
                resMsg.Message = "参数有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (jingjia_ids.EndsWith(","))
                jingjia_ids = jingjia_ids.Substring(0, jingjia_ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(jingjia_ids, ",");
            foreach (var item in id_arr)
            {
                long jingjia_id = ConvertHelper.TryPraseInt(item, 0);
                if (jingjia_id > 0)
                {
                    if (inout == 1)//移除
                    {
                        var jingjiagoods = dbcontext.ADZ_JingjiaMaster.Where(t => t.jingjia_config_id == jingjia_config_id && t.jingjia_id == jingjia_id).ToList();
                        if (jingjiagoods.Any())
                        {
                            jingjiagoods.ForEach(t => t.jingjia_config_id = 0);
                        }
                    }
                    else if (inout == 2)//加入
                    {
                        var jingjiagoods = dbcontext.ADZ_JingjiaMaster.Where(t => t.jingjia_id == jingjia_id && t.jingjia_config_id == 0).ToList();
                        if (jingjiagoods.Any())
                        {
                            jingjiagoods.ForEach(t => t.jingjia_config_id = jingjia_config_id);
                        }
                    }
                }
            }
            dbcontext.SaveChanges();
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }





        [Description("竞价报名列表")]
        [ViewPage]
        public ActionResult BaomingGetList(JingjiaQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var tmplist = dbcontext.ADZ_JingjiaBaomingOrder.Where(t => t.jingjia_config_id == queryModel.jingjia_config_id)
                .OrderByDescending(t => t.is_succ)
                .ThenByDescending(t => t.seq_id).ToList();
            var alllist = new List<ADZ_JingjiaBaomingOrder>();
            if (tmplist.Any())
            {
                foreach (var item in tmplist)
                {
                    bool contains = false;
                    foreach (var allitem in alllist)
                    {
                        if (allitem.uid == item.uid)
                        {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains)
                        alllist.Add(item);
                }
            }
            if(queryModel.jingjia_satus != -10)
            {
                alllist = alllist.Where(t => t.is_succ == queryModel.jingjia_satus).ToList();
            }

            //    .GroupBy(t => t.uid).SelectMany(t => t.Select((b, i) => new { b, i })).Select(m => m.b);

            //数据统计
            int rowcounts = alllist.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = alllist.OrderByDescending(t => t.seq_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_JingjiaBaomingOrder> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_JingjiaBaomingOrder>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("竞价出价列表")]
        [ViewPage]
        public ActionResult ChujiaGetList(JingjiaQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_JingjiaSubmit.Where(t => t.jingjia_id == queryModel.jingjia_id);

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.seq_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_JingjiaSubmit> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_JingjiaSubmit>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }


        [Description("竞价列表")]
        [ViewPage]
        public ActionResult GetList(JingjiaQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_JingjiaMaster.Where(t => t.is_del == 0);
            if (queryModel.jingjia_satus != -10)
            {
                queryable = queryable.Where(t => t.jingjia_satus == queryModel.jingjia_satus);
            }

            if (queryModel.jingjia_config_id > 0)
            {
                queryable = queryable.Where(t => t.jingjia_config_id == queryModel.jingjia_config_id);
            }

            ////成交未付款
            if (queryModel.is_notpay == 1)
            {
                var tt = dbcontext.ADZ_JingjiaSubmit.Where(t => t.is_hit == 1 && t.status != 10).ToList();
                var jingjia_id_list = new List<long>();
                tt.ForEach(t =>
                {
                    jingjia_id_list.Add(t.jingjia_id);
                });
                queryable = queryable.Where(t => t.jingjia_satus == 2 && t.chujia_counts > 0).Where(t => jingjia_id_list.Contains(t.jingjia_id));
            }

            if (!string.IsNullOrWhiteSpace(queryModel.jingjia_name))
                queryable = queryable.Where(t => (t.jingjia_name.Contains(queryModel.jingjia_name)));
            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.create_time >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.create_time < end_time);
            }

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.jingjia_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_JingjiaMaster> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_JingjiaMaster>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("编辑竞价")]
        [ViewPage]
        public ActionResult Add(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_JingjiaMaster model = new ADZ_JingjiaMaster();
            if (id > 0)
            {
                model = dbcontext.ADZ_JingjiaMaster.FirstOrDefault(t => t.jingjia_id == id && t.is_del == 0);
                var config = dbcontext.ADZ_JingjiaConfig.FirstOrDefault(t => t.jingjia_config_id == model.jingjia_config_id);
                if (config != null)
                {
                    if (model != null && !model.real_end_time.HasValue)
                    {
                        model.real_end_time = config.end_time;
                    }
                }
            }
            return View(model);
        }

        [Description("编辑竞价")]
        [HttpPost]
        public ActionResult Add(ADZ_JingjiaMaster model)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            //参数验证
            if (string.IsNullOrWhiteSpace(model.jingjia_name))
            {
                resMsg.Message = "竞价名称不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (model.base_price < 0)
            {
                resMsg.Message = "起拍价错误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (model.step_price <= 0)
            {
                resMsg.Message = "加价幅度错误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (model.stay_miniutes <= 0)
            {
                resMsg.Message = "延时周期错误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (string.IsNullOrWhiteSpace(model.remark))
                model.remark = "";

            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            if (hfc != null && hfc.Count > 0)
            {
                var file = hfc[0];
                var putResult = ImageFileBiz.UpImg(file, "jingjia");
                if (putResult.Item1)
                    model.img_url = putResult.Item3;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(model.img_url))
                    model.img_url = "";
            }

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (model.jingjia_id > 0)
            {
                var old_model = dbcontext.ADZ_JingjiaMaster.FirstOrDefault(t => t.jingjia_id == model.jingjia_id);
                if (old_model != null)
                {
                    List<string> explist = new List<string>();
                    explist.Add("create_time");
                    explist.Add("last_time");
                    explist.Add("last_price");
                    explist.Add("baoming_counts");
                    explist.Add("chujia_counts");
                    explist.Add("weiguan_counts");
                    explist.Add("jingjia_type");
                    explist.Add("is_del");
                    explist.Add("jingjia_id");
                    explist.Add("jingjia_config_id");
                    Utils.Mapper(old_model, model, explist);
                }

                dbcontext.SaveChanges();

            }
            else
            {
                model.create_time = DateTime.Now;
                model.jingjia_type = "增价拍";
                model.last_username = "";
                dbcontext.ADZ_JingjiaMaster.Add(model);
            }

            dbcontext.SaveChanges();

            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("删除竞价")]
        [HttpPost]
        public ActionResult ToDel(int id = 0)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var entity = dbcontext.ADZ_JingjiaMaster.FirstOrDefault(t => t.jingjia_id == id);
            if (entity != null)
            {
                entity.is_del = 1;
                if (dbcontext.SaveChanges() > 0)
                {
                    resMsg.IsSuccess = true;
                    resMsg.Message = "操作成功";
                }
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        #region 竞价商品管理

        [Description("竞价商品列表")]
        [ViewPage]
        public ActionResult editgoods(JingjiaGoodsQueryModel queryModel)
        {
            queryModel.Trim();
            queryModel.pageSize = 1000;
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var goods_ids =
                from t in dbcontext.ADZ_GoodsJingjia
                where t.jingjia_id.Equals(0)
                select t.goods_id;


            //条件筛选
            var queryable = dbcontext.Views_Goods.Where(t => goods_ids.Contains(t.goods_id) && t.is_del == 0);
            if (queryModel.goods_id > 0)
            {
                queryable = queryable.Where(t => t.goods_id == queryModel.goods_id);
            }
            if (queryModel.brand_id != -10)
                queryable = queryable.Where(t => t.brand_id == queryModel.brand_id);
            if (queryModel.supplier_id != -10)
                queryable = queryable.Where(t => t.supplier_id == queryModel.supplier_id);
            if (queryModel.category_id != -10)
            {
                var cateinfo = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == queryModel.category_id && t.is_del == 0);
                if (cateinfo != null)
                {
                    var cateids = from t in dbcontext.ADZ_GoodsCategory
                                  where t.category_code.StartsWith(cateinfo.category_code) && t.is_del.Equals(0)
                                  select t.category_id;
                    queryable = queryable.Where(t => cateids.Contains(t.category_id.Value));
                }
            }
            if (!string.IsNullOrWhiteSpace(queryModel.MPN))
                queryable = queryable.Where(t => t.MPN.StartsWith(queryModel.MPN));

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.goods_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<Views_Goods> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<Views_Goods>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }


        [Description("编辑竞价物料")]
        [HttpPost]
        public ActionResult doeditgoods(int inout, long goods_id, long jingjia_id)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (inout == 0 || goods_id <= 0 || jingjia_id <= 0)
            {
                resMsg.Message = "参数有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (inout == 1)//移除
            {
                var jingjiagoods = dbcontext.ADZ_GoodsJingjia.Where(t => t.goods_id == goods_id && t.jingjia_id == jingjia_id).ToList();
                if (jingjiagoods.Any())
                {
                    jingjiagoods.ForEach(t => t.jingjia_id = 0);
                }
                dbcontext.SaveChanges();
            }
            else if (inout == 2)//加入
            {
                var jingjiagoods = dbcontext.ADZ_GoodsJingjia.Where(t => t.goods_id == goods_id && t.jingjia_id == 0).ToList();
                if (jingjiagoods.Any())
                {
                    jingjiagoods.ForEach(t => t.jingjia_id = jingjia_id);
                }
                dbcontext.SaveChanges();
            }
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        [Description("批量编辑竞价物料")]
        [HttpPost]
        public ActionResult doeditmoregoods(int inout, string goods_ids, long jingjia_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (inout == 0 || string.IsNullOrWhiteSpace(goods_ids) || jingjia_id <= 0)
            {
                resMsg.Message = "参数有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (goods_ids.EndsWith(","))
                goods_ids = goods_ids.Substring(0, goods_ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(goods_ids, ",");
            foreach (var item in id_arr)
            {
                long goods_id = ConvertHelper.TryPraseInt(item, 0);
                if (goods_id > 0)
                {
                    if (inout == 1)//移除
                    {
                        var jingjiagoods = dbcontext.ADZ_GoodsJingjia.Where(t => t.goods_id == goods_id && t.jingjia_id == jingjia_id).ToList();
                        if (jingjiagoods.Any())
                        {
                            jingjiagoods.ForEach(t => t.jingjia_id = 0);
                        }
                        dbcontext.SaveChanges();
                    }
                    else if (inout == 2)//加入
                    {
                        var jingjiagoods = dbcontext.ADZ_GoodsJingjia.Where(t => t.goods_id == goods_id && t.jingjia_id == 0).ToList();
                        if (jingjiagoods.Any())
                        {
                            jingjiagoods.ForEach(t => t.jingjia_id = jingjia_id);
                        }
                        dbcontext.SaveChanges();
                    }
                }
            }
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}